/*
 * Copyright 2009 Jiwei Xu (http://www.xujiwei.com)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

provide("Objects.RectObj");

require("Objects.FigureObj");

RectObj = Class(FigureObj, {
    Initialize : function(start, end) {
        this.Base();

        start = start || {X:0, Y:0};
        end = end || {X:0, Y:0};
        this.Handles = [start,
                        { X : end.X, Y : start.Y },
                        end,
                        { X : start.X , Y : end.Y}];
    },

    Draw : function(ctx) {
        this.SetStyles(ctx);

        var start = this.Handles[0], end = this.Handles[2];
        ctx.beginPath();
        ctx.rect(start.X, start.Y, end.X - start.X, end.Y - start.Y);
        if (this.IsFill) {
            ctx.fill();
        }
        ctx.stroke();
    },

    MoveHandle : function(index, delta) {
        var h = this.Handles;
        if (h[index]) {
            h[index].X += delta.X;
            h[index].Y += delta.Y;
            switch (index) {
                case 0:
                    h[1].Y = h[index].Y;
                    h[3].X = h[index].X;
                    break;
                case 1:
                    h[0].Y = h[index].Y;
                    h[2].X = h[index].X;
                    break;
                case 2:
                    h[3].Y = h[index].Y;
                    h[1].X = h[index].X;
                    break;
                case 3:
                    h[2].Y = h[index].Y;
                    h[0].X = h[index].X;
                    break;
                default:
                    break;
            }

            this.CalcBounds();
        }
    }
});